<?php

namespace app\models;


use app\librarys\DbCacheUtility;
use app\librarys\Tools;

class MerchantGroup
{
    public static function getCount()
    {
        $cmd = \Yii::$app->db->createCommand('select count(1) from t_merchant_group_config');
        $count = DbCacheUtility::queryScalar($cmd);
        return $count;
    }

    public static function getList($page, $pageSize)
    {
        $cmd = \Yii::$app->db->createCommand('select * from t_merchant_group_config ORDER BY create_datetime DESC limit :offset , :limit');
        $cmd->bindValue(':offset', $page * $pageSize);
        $cmd->bindValue(':limit', $pageSize);
        $items = DbCacheUtility::queryAll($cmd);
        return $items;
    }

    public static function getById($id)
    {
        $cmd = \Yii::$app->db->createCommand('select * from t_merchant_group_config where id=:id');
        $cmd->bindValue(':id', $id);
        $item = DbCacheUtility::queryOne($cmd);
        return $item;
    }

    public static function save($id, $code, $name)
    {
        $cmd = \Yii::$app->db->createCommand('
            insert into t_merchant_group_config(id, `code`, `name`)
            values(:id, :code, :name)
            ON DUPLICATE KEY
            UPDATE `code`=:code, `name`=:name;
        ');
        $cmd->bindValue(':id', $id == '0' ? Tools::db_uuid() : $id);
        $cmd->bindValue(':code', $code);
        $cmd->bindValue(':name', $name);
        DbCacheUtility::execute($cmd);
    }

    public static function getMerchantCountByGroupId($merchantGroupId)
    {
        $cmd = \Yii::$app->db->createCommand('select count(1) from t_merchant_group where merchant_group_id=:merchantGroupId');
        $cmd->bindValue(':merchantGroupId', $merchantGroupId);
        $count = DbCacheUtility::queryScalar($cmd);
        return $count;
    }

    public static function getMerchantList($page, $pageSize, $merchantGroupId)
    {
        $cmd = \Yii::$app->db->createCommand('
            select t0.*, t1.`name` as merchant_name from t_merchant_group t0
            LEFT join t_merchant t1 on t1.id=t0.merchant_id
            where t0.merchant_group_id=:merchantGroupId and t0.is_deleted=0
            ORDER BY t0.create_datetime DESC
             limit :offset , :limit');
        $cmd->bindValue(':merchantGroupId', $merchantGroupId);
        $cmd->bindValue(':offset', $page * $pageSize);
        $cmd->bindValue(':limit', $pageSize);
        $items = DbCacheUtility::queryAll($cmd);
        return $items;
    }

    public static function getMerchantConfig($merchant_id)
    {
//        $cmd = \Yii::$app->db->createCommand('select t2.is_auth,t0.`name`,t0.search_range,t0.content_length,t0.image_count,t0.video_size from t_merchant_group_config t0
//                      LEFT JOIN t_merchant_group t1 on t0.id=t1.merchant_group_id LEFT JOIN t_merchant t2 on t2.id=t1.merchant_id  where t1.merchant_id=:merchant_id and t1.`status`=1');
        $cmd = \Yii::$app->db->createCommand('select is_auth from t_merchant where id=:merchant_id');
        $cmd->bindValue(':merchant_id', $merchant_id);
//        var_dump($cmd->rawSql);
        $item = DbCacheUtility::queryOne($cmd);
        return $item;

    }
}